﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for FlightBusiness
/// </summary>
public class FlightBusiness
{
	public FlightBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool Insert(FlightEntities item)
    {
        bool flat = false;
        string[] para = { "@planeID", "@departure", "@arrival", "@startTime", "@endTime", "@departureDate", "@arrivalDate",
                            "@promotionsID","@price","@miles","@smoking","@panelID" };
        string[] values = {  item._planeID.ToString(), item._departure,
                              item._arrival, item._startTime, item._endTime, item._departureDate,
                              item._arrivalDate, item._promotionsID.ToString(),item._price.ToString(),
                              item._miles.ToString(),item._smoking.ToString(),item._panelID.ToString() };
        if (DataAccess.getExecProcedure("sp_InsertFlight", para, values) > 0)
        {
            flat = true;
        }
        else
            flat = false;
        return flat;
    }

    public bool Insert(string[] values)
    {
        bool flat = false;
        string[] para = { "@planeID", "@departure", "@arrival", "@startTime", "@endTime", "@departureDate", "@arrivalDate",
                            "@promotionsID","@price","@miles","@smoking" };
        
        if (DataAccess.getExecProcedure("sp_InsertFlight", para, values) > 0)
        {
            flat = true;
        }
        else
            flat = false;
        return flat;
    }

    public bool Edit(FlightEntities item)
    {
        bool flat = false;
        string[] para = { "@flightID", "@planeID", "@departure", "@arrival", "@startTime", "@endTime", "@departureDate", "@arrivalDate",
                            "@promotionsID","@price","@miles","@smoking" };
        string[] values = { item._flightID.ToString(),item._flightID.ToString(), item._planeID.ToString(), item._departure,
                              item._arrival, item._startTime, item._endTime, item._departureDate,
                              item._arrivalDate, item._promotionsID.ToString(),item._price.ToString(),
                              item._miles.ToString(),item._smoking.ToString() };
        if (DataAccess.getExecProcedure("sp_UpdateFlight", para, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Edit(string[] values)
    {
        bool flat = false;
        string[] para = { "@flightID", "@planeID", "@departure", "@arrival", "@startTime", "@endTime", "@departureDate", "@arrivalDate",
                            "@promotionsID","@price","@miles","@smoking" };
        
        if (DataAccess.getExecProcedure("sp_UpdateFlight", para, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Delete(int id)
    {
        if (DataAccess.getExec("exec sp_DeleteFlight '"+id+"'") > 0)
            return true;
        else
            return false;
    }

    public DataTable searchFlight(string noidi,string noiden,string ngaydi)
    {
        return DataAccess.DataTable_Query("exec sp_SearchFlight '" + noidi + "','" + noiden + "','" + ngaydi + "'");

    }

    public SqlDataReader searchFlightReader(string noidi, string noiden, string ngaydi)
    {
        return DataAccess.DataReader_getReader("exec sp_SearchFlight '" + noidi + "','" + noiden + "','" + ngaydi + "'");

    }
    
    public SqlDataReader getFlightInDay(string currentDay)
    {
        return DataAccess.DataReader_getReader("exec sp_GetFlightInDay '" + currentDay + "'");
    }

    public DataTable getAllFlight()
    {
        return DataAccess.DataTable_Query("exec sp_GetAllFlight '0'");
    }

    public DataTable getFlightDataTable(int id)
    {
        return DataAccess.DataTable_Query("exec sp_GetAllFlight '" + id + "'");
    }

    public FlightEntities getFlight(int id)
    {
        FlightEntities fe = new FlightEntities();
        DataTable dt = DataAccess.DataTable_Query("exec sp_GetAllFlight '" + id + "'");
        if (dt != null)
        {
            DataRow dr = dt.Rows[0];
            fe._flightID =int.Parse(dr["flightID"].ToString());
            fe._planeID = int.Parse(dr["planeID"].ToString());
            fe._departure = dr["departure"].ToString();
            fe._arrival = dr["arrival"].ToString();
            fe._startTime = dr["startTime"].ToString();
            fe._endTime = dr["endTime"].ToString();
            fe._departureDate = dr["departureDate"].ToString();
            fe._arrivalDate = dr["arrivalDate"].ToString();
            fe._promotionsID = int.Parse(dr["promotionsID"].ToString());
            fe._price = Convert.ToDouble(dr["price"].ToString());
            fe._miles = Convert.ToDouble(dr["miles"].ToString());
            fe._smoking=bool.Parse(dr["smoking"].ToString());
        }
        return fe;
    }

    public DataTable getSelectFlight(string noidi,string noiden,string ngaydi)
    {
        return DataAccess.DataTable_Query("sp_GetSelectFlight '"+noidi+"','"+noiden+"','"+ngaydi+"'");
    }

    public DataTable getFlightDetail(string noidi, string noiden, string ngaydi)
    {
        return DataAccess.DataTable_Query("exec sp_DetailFlight '" + noidi + "','" + noiden + "','" + ngaydi + "'");
    }

    public DataTable getFlightBy(string[] TableName, string[] fields, string Criterias, string priorities)
    {
        DataTable dt = new DataTable();
        string query = SQLQuery.SQLQuery_SELECT(TableName, fields, Criterias, priorities);
        return dt = DataAccess.DataTable_Query(query);
    }
}